<?php

namespace app\api\controller;

use app\common\controller\Api;


use app\server\UserServer;

use GatewayClient\Gateway;

use Redis;
use think\Env;


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function __construct()
    {
        parent::__construct();
        Gateway::$registerAddress = '127.0.0.1:1238';

    }
    
    public function _initialize()
    {

        parent::_initialize();
    }


    public function addUser()
    {
        $client_id = $this->request->param("client_id");
        $uid = $this->request->param("uid");
        Gateway::bindUid($client_id, $uid);
        $this->success('请求成功',$client_id);
    }

    public function sendToUid()
    {
        $uid = $this->request->param("uid");
        $msg = $this->request->param("msg");
        $data = [
            "action"=>"sendToUid",
            "msg"=>$msg
        ];
        Gateway::sendToUid($uid, json_encode($data));
        $this->success('请求成功', $data);
    }

    public function redis()
    {
        $config = config('redis');
        var_dump($config);
        $redis = new Redis();
       # $redis->connect($config['host'], intval($config['port']), $config['timeout']);
        #pconnect($this->options['host'], intval($this->options['port']), $this->options['timeout'], 'persistent_id_' . $this->options['select']);
        if(!$config['persistent']){
            #密码怎么设置
            $redis->connect($config['host'], intval($config['port']), $config['timeout']);
        }else{
            $redis->pconnect($config['host'], intval($config['port']), $config['timeout'], 'persistent_id_' . $config['select']);
        }
       
        if ('' != $config['password']) {
            $redis->auth($config['password']);
        }

        if (0 != $config['select']) {
            $redis->select(intval($config['select']));
        }

        $name = $this->request->param("name");
        $redis->set('name', $name);
        $name = $redis->get('name');
        $this->success('请求成功',$name);
    }

    public function test()
    {
        $a = Env::get('workerman.registerAddress', '127.0.0.1:1238');

        return $a;
    }

    public function add()
    {
        $client_id = $this->request->param('client_id');
        
        // client_id与uid绑定
        Gateway::bindUid($client_id, "1");
        // 加入某个群组（可调用多次加入多个群组）
        Gateway::joinGroup($client_id, "1");
        $this->success('请求成功',$client_id);

    }


    public function sendmsg()
    {
        $client_id = $this->request->param('client_id');
        $msg = $this->request->param('msg');
        $data = [
            "action"=>"sendmsg",
            "msg"=>$msg,
            "client_id"=>$client_id
        ];
        Gateway::sendToClient($client_id, $data);
        $this->success('请求成功',$data);
    }


    public function sendGroupMsg()
    {
        $group = $this->request->param('group');
        $msg = $this->request->param('msg');
        $data = [
            "action"=>"sendGroupMsg",
            "msg"=>$msg
        ];
        Gateway::sendToGroup($group, $data);
        $this->success('请求成功',$data);
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $action = $this->request->param('action');
        $msg = $this->request->param('msg');
        $client_id = $this->request->param('client_id');
        #接收到信息，通过workerman发送给客户端

        $data = [
            "action"=>$action,
            "msg"=>$msg
        ];




        // Gateway::sendToClient($client_id, $data);
        // Gateway::closeClient($client_id);
        // Gateway::isOnline($client_id);
        // Gateway::bindUid($client_id, $uid);
        // Gateway::isUidOnline($uid);
        // Gateway::isUidsOnline($uids);
        // Gateway::getClientIdByUid($uid);
        // Gateway::unbindUid($client_id, $uid);
        // Gateway::sendToUid($uid, $dat);
        // Gateway::joinGroup($client_id, $group);
        // Gateway::sendToGroup($group, $data);
        // Gateway::leaveGroup($client_id, $group);
        // Gateway::getClientCountByGroup($group);
        // Gateway::getClientSessionsByGroup($group);
        // Gateway::getAllClientCount();
        // Gateway::getAllClientSessions();
        // Gateway::setSession($client_id, $session);
        // Gateway::updateSession($client_id, $session);
        // Gateway::getSession($client_id);

        $this->success('请求成功',$data);
    }
}
